﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
namespace BLL
{
    public class HomeBll
    {
        static private Random r = new Random();

        //private int MaxSkip = r.Next(0, 30);//首页的排名为随机获取
        private int MaxTake = 3;
        private int MaxAllThingTake = 3;

        public List<User> getUsersByMark()
        {
            var context = new ModelContainer();
           // List<User> LU= new List<>();
            return null;

        }
        public List<Course> getCourseByRecommend(int MaxSkip)
        {
            var context = new ModelContainer();
            return context.Courses
                .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                .OrderByDescending(course => course.CourseInfo.RcmdNum)
                .Skip(MaxSkip)
                .Take(MaxTake).ToList();
        }
        public List<Course> getCourseByVisit(int MaxSkip)
        {
            var context = new ModelContainer();
            return context.Courses
                .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                .OrderByDescending(course => course.CourseInfo.Visits)
                .Skip(MaxSkip)
                .Take(MaxTake).ToList();
        }

        public List<Course> getCourseByGold(int MaxSkip)
        {
            var context = new ModelContainer();
            return context.Courses
                .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                .OrderByDescending(course => course.CourseInfo.Gold)
                .Skip(MaxSkip)
                .Take(MaxTake).ToList();
        }

        public List<Course> getCourseByMark(int MaxSkip)
        {
            var context = new ModelContainer();
            return context.Courses
                .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                .OrderByDescending(course => course.CourseInfo.Mark)
                .Skip(MaxSkip)
                .Take(MaxTake).ToList();
        }

        public List<AllThing> getAllThing(int pageNum)
        {
            var context = new ModelContainer();
            return context.AllThings
                .OrderByDescending(things => things.Time)
                .Skip(pageNum*MaxAllThingTake)
                .Take(MaxAllThingTake).ToList();
        }
        public List<UserThing> getCoursesUserThingByCUserVisit(string UserId)
        {
            var context = new ModelContainer();
            return context.UserThings
                .Where(userThing => userThing.UserId == UserId)
                .OrderByDescending(userThing => userThing.Visits)
                .ToList();
        }

        public List<Course> getCoursesByCUserVisit(string UserId)
        {
            List<UserThing> thingList = getCoursesUserThingByCUserVisit(UserId);
            List<Course> courseList = new List<Course>(0);
            var context = new ModelContainer();
            int count = 0;
            foreach (var userThing in thingList)
            {
                int CUId = int.Parse(userThing.CourseId);
                var course = context.Courses
                    .Where(courses => courses.ID == CUId).Take(MaxTake).ToList();
                courseList.AddRange(course);
                count++;
                if (count > 2)
                    break;
            }
            return courseList;
        }

        public void cleanVisits(string UserId)
        {
            var context = new ModelContainer();
            var t = context.UserThings
                .Where(uT => uT.UserId == UserId);
            foreach (var userThing in t)
            {
                userThing.Visits = 0;
            }
            //context.Attach(t);
            context.AcceptAllChanges();

        }
    }
}
